import { getApproprateEnergy, getCreepActivityCoeficient, getMaxEnergy, getMinEnergy } from "../role/spawn";
import { errLog } from "../util/log";

export const globalLog={
    run(){
        if(Game.time%20==3){
            this.printLog();
        }
    },
    printLog(){
        try{
            let logMap={};
            let maxRoleNameLength=0;
            for(let name in Game.creeps){
                if(!Game.creeps[name]){
                    continue;
                }
                let role=Game.creeps[name].memory.role;
                if(role==undefined){
                    continue;
                }
                if(role.length>maxRoleNameLength){
                    maxRoleNameLength=role.length;
                }
                if(logMap[role]==undefined){
                    logMap[role]=1;
                    continue;
                }

                logMap[role]++;
            }
            let logStr='';
            logStr+='\ntry to spawn:'// '+;
            for(let roomName in Game.rooms){
                let room = Game.rooms[roomName];
                if(!room || !room.controller || !room.controller.my){
                    continue;
                }
                let tryToSpawn = Memory.rooms[room.name].tryToSpawn;
                logStr+='\n\t' + roomName +'('+getMinEnergy(room)+'/'+getApproprateEnergy(room)+':'+getMaxEnergy(room)+'): ' + (tryToSpawn ? tryToSpawn : "NOTHING");
            }
            logStr+='\nroles: ';
            for(let role in logMap){
                let tabLength=maxRoleNameLength-role.length;

                logStr+='\n\t'+role+': ';
                for(let i=0; i<tabLength; i++){
                    logStr+=' ';
                }
                logStr+=logMap[role];
            }

            logStr+='\nGCAC(group creep activity coeficient):'
            for(let roomName in Game.groups){
                let group = Game.groups[roomName];
                logStr+='\n\t'+roomName+': '+getCreepActivityCoeficient(group.spawnRoom, true);
            }
            let enemyRoomNames=[];
            for(let roomName in Memory.rooms){
                if(Memory.rooms[roomName].enemy){
                    enemyRoomNames.push(roomName);
                }
            }
            if(enemyRoomNames.length>0){
                if(enemyRoomNames.length===1){
                    let room = Game.rooms[enemyRoomNames[0]];
                    logStr+='\nenemy: '+enemyRoomNames[0];
                    if(room){
                        if(room.controller && room.controller.owner){
                            logStr += ': '+room.controller.owner.username;
                        }else{
                            logStr += ': Invader';
                        }
                    }
                }else{
                    logStr+='\nenemies:';
                    for(let roomName of enemyRoomNames){
                        let room = Game.rooms[roomName];
                        logStr += '\n\t' + roomName;
                        if(room){
                            if(room.controller && room.controller.owner){
                                logStr += ': '+room.controller.owner.username;
                            }else{
                                logStr += ': Invader';
                            }
                        }
                    }
                }
            }


            if(logStr.length>0){
                console.log(logStr);
            }
        }catch(e){
            errLog(e);
        }
    }
}
